package com.Exam;

import java.util.concurrent.*;

public class Exam04 {
    //使⽤ ThreadPoolExecutor 分别实现 Executors 的 newCachedThreadPool 、
    //FixedThreadPool 还有 SingleThreadExecutor 并测试
    public static void main(String[] args) {
        ExecutorService executor = Executors.newCachedThreadPool();
        executor.execute(() ->{
            int sum =0;
            for (int i = 0; i <= 5; i++) {
                sum += i;
            }
            System.out.println(Thread.currentThread().getName());
            System.out.println(sum);
        });
        executor.shutdown();

        //实现的是Callable方法
        ExecutorService service = Executors.newFixedThreadPool(10);
        Future<Integer> submit = service.submit(() ->{
            int sum =0;
            for (int i = 0; i <= 10; i++) {
                sum += i;
            }
            System.out.println(Thread.currentThread().getName());
            return sum;
        });
        try {
            System.out.println(submit.get());
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
        service.shutdown();

        //实现的是Runnable方法
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                int sum = 0;
                for (int i = 0; i <= 100; i++) {
                    sum += i;
                }
                System.out.println(Thread.currentThread().getName());
                System.out.println(sum);
            }
        };
        runnable.run();
        executorService.shutdown();
    }
}
class CachedThreadPool {
    public static void main(String[] args) {
        // 使用`ThreadPoolExecutor`
        // 分别实现`Executors`的`newCachedThreadPool`、`FixedThreadPool`还有`SingleThreadExecutor`并测试。
        ExecutorService service = new ThreadPoolExecutor(
                0,
                Integer.MAX_VALUE,
                60,
                TimeUnit.SECONDS,
                new SynchronousQueue<>()
        );
        for (int i = 0; i < 10; i++) {
            service.execute(() -> {
                System.out.println(Thread.currentThread().getName());
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }

        service.shutdown();

    }
}
class FixedThreadPool {

    public static void main(String[] args) {
        ThreadPoolExecutor service = new ThreadPoolExecutor(
                3,
                3,
                0,
                TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>() // ĬInteger.MAX_VALUE
        );

        for (int i = 0; i < 10; i++) {
            service.execute(() -> {
                System.out.println(Thread.currentThread().getName());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }

    }
}
class SingleThreadPool {

    public static void main(String[] args) {
        ThreadPoolExecutor service = new ThreadPoolExecutor(
                1,
                1,
                0,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>()
        );
        for (int i = 0; i < 10; i++) {
            service.execute(() -> {
                System.out.println(Thread.currentThread().getName());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }

    }
}